package com.tedu.ivos.vehicle.service.impl;

import com.tedu.ivos.vehicle.mapper.VehicleMapper;
import com.tedu.ivos.vehicle.pojo.dto.VehicleParam;
import com.tedu.ivos.vehicle.pojo.dto.VehicleQuery;
import com.tedu.ivos.vehicle.pojo.entity.Vehicle;
import com.tedu.ivos.vehicle.pojo.vo.VehicleVO;
import com.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class VehicleServiceImpl implements VehicleService {

    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<VehicleVO> selectVehicle(VehicleQuery vehicleQuery) {
        log.debug("查询车辆信息业务：{}", vehicleQuery);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);

        return list;
    }

    @Override
    public void vehicleInsert(VehicleParam vehicleParam) {
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleParam, vehicle);
        if (vehicle.getId() == null){
            log.debug("新增车辆业务：{}", vehicleParam);
            vehicle.setCreateTime(new Date());
            vehicle.setStatus("1");
            vehicle.setGeofenceBindStatus("0");
            vehicleMapper.insertVehicle(vehicle);
        }else{
            log.debug("更新车辆业务：{}", vehicleParam);
            vehicle.setUpdateTime(new Date());
            vehicleMapper.updateVehicle(vehicle);
        }

    }

    @Override
    public void vehicleDelete(Long id) {
        log.debug("删除车辆业务：{}", id);
        vehicleMapper.deleteVehicle(id);
    }

    @Override
    public void unbindVehicle(Long id) {
        log.debug("解绑车辆业务：{}", id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("0");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(null);
        vehicleMapper.updateNullValue(vehicle);
    }

    @Override
    public void bindVehicle(Long geoId, Long id) {
        log.debug("绑定车辆业务：{},{}", geoId, id);
        Vehicle vehicle = new Vehicle();
        vehicle.setId(id);
        vehicle.setGeofenceBindStatus("1");
        vehicle.setUpdateTime(new Date());
        vehicle.setGeofenceId(geoId);
        vehicleMapper.updateVehicle(vehicle);
    }

}
